﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.Office.DocumentManagement.DocumentSets;

namespace DR5.SPExtend
{
    public static class DocumentSetsExtensions
    {
        public static bool IsDocumentSetChildItem(this SPListItem item)
        {
            bool documentSetItem = false;
            DocumentSet documentSet = null;

            if (null != item && null != item.File)
            {
                documentSet = DocumentSet.GetDocumentSet(item.File.ParentFolder);

                if (null != documentSet)
                    documentSetItem = true;
            }
            return documentSetItem;
        }

        /// <summary>
        /// Get Doucment Set of SPListItem
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public static DocumentSet GetParentDocumentSetItem(this SPListItem item)
        {
            DocumentSet documentSet = null;

            if (null != item && null != item.File)
            {
                documentSet = DocumentSet.GetDocumentSet(item.File.ParentFolder);
            }
            return documentSet;
        }

        public static bool IsDocumentSet(this SPListItem item)
        {
            bool documentSetItem = false;
            DocumentSet documentSet = null;

            if (null != item && item.Folder != null)
            {
                documentSet = DocumentSet.GetDocumentSet(item.Folder);

                if (null != documentSet)
                    documentSetItem = true;
            }
            return documentSetItem;
        }

        public static DocumentSet GetDocumentSet(this SPListItem item)
        {
            DocumentSet documentSet = null;

            if (null != item && item.Folder != null)
            {
                documentSet = DocumentSet.GetDocumentSet(item.Folder);
            }
            return documentSet;
        }

        public static bool IsDocumentSet(this SPFolder folder)
        {
            bool documentSetItem = false;
            DocumentSet documentSet = null;

            if (null != folder)
            {
                documentSet = DocumentSet.GetDocumentSet(folder);

                if (null != documentSet)
                    documentSetItem = true;
            }
            return documentSetItem;
        }
    }
}
